//
//  BIDTransmitEmailViewController.m
//  shangwuting
//
//  Created by mal on 13-12-26.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDTransmitEmailViewController.h"
#import "BIDMemberListViewController.h"
#import "BIDLoginViewController.h"
#import "BIDCustomTextView.h"

//查看
static NSString *emailDetailURL = @"emailAndroid!toInboxForward.action";
//转发
static NSString *transmitEmailURL = @"emailAndroid!sendForward.action";

@interface BIDTransmitEmailViewController ()<BIDMemberListViewControllerDelegate>
{
}
@end

@implementation BIDTransmitEmailViewController
@synthesize receiverTF;
@synthesize titleTF;
@synthesize contentTextView;
@synthesize myTableView;
@synthesize bgImgView1;
@synthesize transmitBtn;
@synthesize receiverIds;
@synthesize emailId;
@synthesize itemsArr;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"转发邮件";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    contentTextView.inputAccessoryView = self.toolBar;
    contentTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    contentTextView.layer.borderWidth = 1.0f;
    contentTextView.layer.cornerRadius = 5;
    [contentTextView setBackgroundColor:[UIColor clearColor]];
    
    [receiverTF setBackgroundColor:[UIColor clearColor]];
    [titleTF setBackgroundColor:[UIColor clearColor]];
    
    myTableView.allowsSelection = NO;
    myTableView.tableFooterView = [[UIView alloc] init];
    
    bgImgView1.frame = myTableView.frame;
    myTableView.backgroundView = bgImgView1;
    //
    [receiverTF setBackgroundColor:[UIColor clearColor]];
    receiverTF.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    receiverTF.layer.borderWidth = 1.f;
    receiverTF.inputAccessoryView = self.toolBar;
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar.png"]];
    receiverTF.rightView = imgView;
    receiverTF.rightViewMode = UITextFieldViewModeAlways;
    receiverTF.delegate = self;
    //
    [titleTF setBackgroundColor:[UIColor clearColor]];
    titleTF.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    titleTF.layer.borderWidth = 1.f;
    titleTF.inputAccessoryView = self.toolBar;
    //
    contentTextView.layer.borderColor = RGBACOLOR(101.f, 161.f, 243.f, 1.f).CGColor;
    contentTextView.layer.borderWidth = 1;
    contentTextView.layer.cornerRadius = 3;
    contentTextView.placeholder = @"请输入邮件内容";
    contentTextView.inputAccessoryView = self.toolBar;
    [contentTextView setFont:[UIFont systemFontOfSize:[AppDelegate getFontSize]]];
    [contentTextView setBackgroundColor:[UIColor clearColor]];
    //调整布局
    [self adjustLayout];
    
    [self loadContent];
}

//加载数据
- (void)loadContent
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], emailDetailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@", emailId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            titleTF.text = [responseDic objectForKey:@"title"];
            contentTextView.text = [responseDic objectForKey:@"content"];
            NSArray *arr = [responseDic objectForKey:@"docs"];
            itemsArr = [NSMutableArray arrayWithArray:arr];
            [myTableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    receiverTF = nil;
    titleTF = nil;
    contentTextView = nil;
    myTableView = nil;
    transmitBtn = nil;
    bgImgView1 = nil;
}

- (IBAction)tfEditEnd:(id)sender
{
    [titleTF resignFirstResponder];
}

//转发
- (IBAction)transmitBtnHandler:(id)sender
{
    if(receiverTF.text.length==0)
    {
        [BIDGlobalMethods showAlertView:@"请先选择接收人" buttonTitle:@"关闭" delegate:nil tag:0];
        return;
    }
    NSString *strTitle = titleTF.text;
    strTitle = titleTF.text.length>0?strTitle:@"";
    NSString *strContent = contentTextView.text;
    strContent = contentTextView.text.length>0?strContent:@"";
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], transmitEmailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@&receiveId=%@&userId=%@&title=%@&content=%@", emailId, receiverIds, [AppDelegate getUserId], strTitle, strContent];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            NSNumber *value = [responseDic objectForKey:@"isSuccess"];
            if([value intValue]==1)
            {
                [BIDGlobalMethods showAlertView:@"转发成功" buttonTitle:@"关闭" delegate:self tag:1];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"转发失败" buttonTitle:@"关闭" delegate:nil tag:0];
            }
        }
    }];
}

//调整布局
- (void)adjustLayout
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGRect ownFrame = self.view.frame;
    ownFrame.size.height = screenSize.height-64;
    self.view.frame = ownFrame;
    
    CGRect frame = transmitBtn.frame;
    frame.origin.y = ownFrame.size.height-8-frame.size.height;
    transmitBtn.frame = frame;
    
    frame = myTableView.frame;
    frame.origin.y = transmitBtn.frame.origin.y-8-frame.size.height;
    myTableView.frame = frame;
    
    frame = contentTextView.frame;
    frame.size.height = myTableView.frame.origin.y - 8 - frame.origin.y;
    contentTextView.frame = frame;
}

#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField.tag==1)
    {
        [textField resignFirstResponder];
        BIDMemberListViewController *vc = [[BIDMemberListViewController alloc] init];
        vc.chooseType = TYPE_CHECK;
        vc.delegate = self;
        vc.bShowAllOption = YES;
        vc.rightNavigationItemTitle = @"确定";
        vc.choosedMemberIdArr = [receiverIds componentsSeparatedByString:@","];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - BIDMemberListViewController
- (void)getChoosedMemberIdForNotice:(NSString *)ids andNames:(NSString *)names
{
    receiverTF.text = names;
    receiverIds = ids;
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [itemsArr count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    NSUInteger row = [indexPath row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary *dictionary = [itemsArr objectAtIndex:row];
    NSString *strFileName = [dictionary objectForKey:@"fileName"];
    NSString *strFileSize = [dictionary objectForKey:@"size"];
    strFileSize = [[NSString alloc] initWithFormat:@"(%@)", strFileSize];
    NSUInteger len1 = strFileName.length;
    NSUInteger len2 = strFileSize.length;
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[[NSString alloc] initWithFormat:@"%@%@", strFileName, strFileSize]];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, len1)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:47.0f/255.0f green:132.0f/255.0f blue:61.0f/255.0f alpha:1.0f] range:NSMakeRange(len1, len2)];
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, len1)];
    cell.textLabel.attributedText = str;
    cell.textLabel.font = [UIFont systemFontOfSize:15.0f];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag==1)
    {
        //转发成功
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
